package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IUserService userService;
   
    @Autowired
    private IStatementItemService statementItemService;
    //明细编辑
    @RequiresPermissions("business:statementItem:itemDetail")
    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId,Model model){
        Statement statement = statementService.getById(statementId);
        model.addAttribute("statementItem",statement);
        if (statement.getStatus().equals(Statement.STATUS_CONSUME)){
            return prefix+"editDetail";
        }else {
            statement.setPayee(userService.get(statement.getPayeeId()));
            return prefix +"showDetail";
        }
    }
    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:serviceItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo){
        return statementItemService.query(qo);
    }

    @RequiresPermissions("business:statementItem:saveItems")
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem>items){
        statementItemService.saveDetail(items);
        return AjaxResult.success();
    }
    @RequestMapping("/payStatementItem")
    @ResponseBody
    public AjaxResult payStatementItem(Long statementId){
        statementService.updatePayStatementItem(statementId);
        return AjaxResult.success();
    }

}
